<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>基本信息</span>
          </div>
        </template>
        <el-form label-position="right" :model="candidateForm" :label-width="formLabelWidth" disabled="disabled">
          <el-row>
            <el-col :span="11">
              <el-form-item label="用户名">
                <el-input v-model="candidateForm.username"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="姓名">
                <el-input v-model="candidateForm.name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="密码">
                <el-input v-model="candidateForm.password"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="性别">
                <el-select v-model="candidateForm.gender" placeholder="请选择性别">
                  <el-option label="男" :value="1"></el-option>
                  <el-option label="女" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="出生日期">
                <el-input v-model="candidateForm.birthDate"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11">
              <el-form-item label="联系电话">
                <el-input v-model="candidateForm.contactPhone"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="邮箱">
                <el-input v-model="candidateForm.email"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11">
            <el-form-item label="省/市/区">
              <el-cascader
                v-model="provinceCityArea"
                :options="provinceCityAreaOptions"
                :props="{ expandTrigger: 'hover', value: 'code', label: 'name' }" />
            </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="家庭地址">
                <el-input v-model="candidateForm.address"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card>
        <template #header>
          <div class="card-header">
            <span>教育经历</span>
          </div>
        </template>
        <el-table
          :data="educationState.tableData"
          border
          v-loading="educationState.loading"
          size="small"
          header-cell-class-name="table-header-cell-font"
          cell-class-name="table-cell-font">
          <el-table-column prop="candidateName" label="应聘者姓名"></el-table-column>
          <el-table-column prop="schoolName" label="学校名称"></el-table-column>
          <el-table-column prop="degree" label="学历">
            <template v-slot="{ row }">
              <div v-if="row.degree === 10">初中</div>
              <div v-if="row.degree === 20">高中</div>
              <div v-if="row.degree === 30">本科</div>
              <div v-if="row.degree === 40">硕士研究生</div>
              <div v-if="row.degree === 50">博士研究生</div>
            </template>
          </el-table-column>
          <el-table-column prop="startDate" label="开始日期"></el-table-column>
          <el-table-column prop="endDate" label="结束日期"></el-table-column>
        </el-table>
      </el-card>
      <el-card>
        <template #header>
          <div class="card-header">
            <span>工作经历</span>
          </div>
        </template>
        <el-table
          :data="workExperienceState.tableData"
          border
          v-loading="workExperienceState.loading"
          size="small"
          header-cell-class-name="table-header-cell-font"
          cell-class-name="table-cell-font">
          <el-table-column prop="candidateName" label="应聘者姓名"></el-table-column>
          <el-table-column prop="company" label="公司名称"></el-table-column>
          <el-table-column prop="position" label="岗位"></el-table-column>
          <el-table-column prop="startDate" label="开始日期"></el-table-column>
          <el-table-column prop="endDate" label="结束日期"></el-table-column>
          <el-table-column prop="responsibilities" label="岗位职责"></el-table-column>
        </el-table>
      </el-card>
      <el-card>
        <template #header>
          <div class="card-header">
            <span>技能证书</span>
          </div>
        </template>
        <el-table
          :data="skillCertificateState.tableData"
          border
          v-loading="skillCertificateState.loading"
          size="small"
          header-cell-class-name="table-header-cell-font"
          cell-class-name="table-cell-font">
          <el-table-column prop="candidateName" label="应聘者姓名"></el-table-column>
          <el-table-column prop="certificateName" label="证书名称"></el-table-column>
          <el-table-column prop="issuingAuthority" label="颁发机构"></el-table-column>
          <el-table-column prop="issueDate" label="颁发时间"></el-table-column>
          <el-table-column prop="expiryDate" label="到期日期"></el-table-column>
        </el-table>
      </el-card>
      <el-card>
        <template #header>
          <div class="card-header">
            <span>培训记录</span>
          </div>
        </template>
        <el-table
          :data="trainingRecordState.tableData"
          border
          v-loading="trainingRecordState.loading"
          size="small"
          header-cell-class-name="table-header-cell-font"
          cell-class-name="table-cell-font">
          <el-table-column prop="candidateName" label="应聘者姓名"></el-table-column>
          <el-table-column prop="trainingName" label="培训名称"></el-table-column>
          <el-table-column prop="trainingDate" label="培训日期"></el-table-column>
          <el-table-column prop="durationHours" label="培训时长"></el-table-column>
          <el-table-column prop="provider" label="培训供应商"></el-table-column>
          <el-table-column prop="trainingDescription" label="培训内容"></el-table-column>
        </el-table>
      </el-card>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="12" :offset="6">
          <div class="form-button-container">
            <el-button @click="onBack" type="primary">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineEmits, defineProps, onMounted, reactive, ref, inject, toRefs} from 'vue';
import candidateApi from '@/api/candidateApi'
import type {Candidate} from "@/types/resp/candidate";
import type {Id} from "@/types/id"
import areas from "@/locales/area.json";
import type { Education } from '@/types/resp/education';
import educationApi from '@/api/educationApi';
import type { EducationQueryForm } from '@/types/req/educationQueryForm';
import type { Page } from '@/types/page';
import type { Result } from '@/types/result';
import type { WorkExperienceQueryForm } from '@/types/req/workExperienceQueryForm';
import type { WorkExperience } from '@/types/resp/workExperience';
import workExperienceApi from '@/api/workExperienceApi';
import type { SkillCertificate } from '@/types/resp/skillCertificate';
import skillCertificateApi from '@/api/skillCertificateApi';
import type { SkillCertificateQueryForm } from '@/types/req/skillCertificateQueryForm';
import type { TrainingRecord } from '@/types/resp/trainingRecord';
import trainingRecordApi from '@/api/trainingRecordApi';
import type { TrainingRecordQueryForm } from '@/types/req/trainingRecordQueryForm';

const candidateForm = reactive<Partial<Candidate>>({});
const props = defineProps<Id>()
const provinceCityArea = ref<string[]>([])
const provinceCityAreaOptions = areas.provinces
const emit = defineEmits<{
  (e: 'closeCandidateViewAllEvent'): void;
}>()
const formLabelWidth = ref<string>('')

onMounted(() => {
  initCandidateById(props.id)
  const globalState = inject('globalState', {} as { formLabelWidth?: string })
  if (!globalState) {
    throw new Error('globalState is not provided');
  }
  const globalStateRefs = toRefs(globalState)
  if (globalStateRefs.formLabelWidth) {
    formLabelWidth.value = globalStateRefs.formLabelWidth.value || ''
  }
  getEducationPageData()
  getWorkExperiencePageData()
  getSkillCertificatePageData()
  getTrainingRecordPageData()
})

const initCandidateById = async (id: number) => {
  const resp = await candidateApi.findById(id)
  if (resp && resp.code === 1 && resp.data) {
    Object.assign(candidateForm, resp.data)
    provinceCityArea.value = []
    if (candidateForm.province) {
      provinceCityArea.value.push(candidateForm.province)
    }
    if (candidateForm.city) {
      provinceCityArea.value.push(candidateForm.city)
    }
    if (candidateForm.area) {
      provinceCityArea.value.push(candidateForm.area)
    }
  }
}

const educationState = reactive({
  loading: false,
  tableData: [] as Education[], // 数据列表
  total: 0, // 总条数
  currentPage: 1, // 当前页
  pageSize: 1000
})
const getEducationPageData = async () => {
  try {
    educationState.loading = true
    const resp: Result<Page<Education>> = await educationApi.find({candidateId: props.id} as EducationQueryForm)
    if (resp && resp.code === 1) {
      if (resp.data) {
      const page: Page<Education> = resp.data
        if (page && page.list.length > 0) {
          educationState.tableData = page.list
          educationState.total = page.total
        }
      }
    }
  } catch (e) {
    console.log('获取数据异常', e)
  } finally {
    educationState.loading = false
  }
}

const workExperienceState = reactive({
  loading: false,
  tableData: [] as WorkExperience[],
  total: 0,
  currentPage: 1,
  pageSize: 1000
})
const getWorkExperiencePageData = async () => {
  try {
    workExperienceState.loading = true
    const resp: Result<Page<WorkExperience>> = await workExperienceApi.find({candidateId: props.id} as WorkExperienceQueryForm)
    if (resp && resp.code === 1) {
      if (resp.data) {
      const page: Page<WorkExperience> = resp.data
        if (page && page.list.length > 0) {
          workExperienceState.tableData = page.list
          workExperienceState.total = page.total
        }
      }
    }
  } catch (e) {
    console.log('获取数据异常', e)
  } finally {
    workExperienceState.loading = false
  }
}

const skillCertificateState = reactive({
  loading: false,
  tableData: [] as SkillCertificate[],
  total: 0,
  currentPage: 1,
  pageSize: 1000
})
const getSkillCertificatePageData = async () => {
  try {
    skillCertificateState.loading = true
    const resp: Result<Page<SkillCertificate>> = await skillCertificateApi.find({candidateId: props.id} as SkillCertificateQueryForm)
    if (resp && resp.code === 1) {
      if (resp.data) {
      const page: Page<SkillCertificate> = resp.data
        if (page && page.list.length > 0) {
          skillCertificateState.tableData = page.list
          skillCertificateState.total = page.total
        }
      }
    }
  } catch (e) {
    console.log('获取数据异常', e)
  } finally {
    skillCertificateState.loading = false
  }
}

const trainingRecordState = reactive({
  loading: false,
  tableData: [] as TrainingRecord[],
  total: 0,
  currentPage: 1,
  pageSize: 1000
})
const getTrainingRecordPageData = async () => {
  try {
    trainingRecordState.loading = true
    const resp: Result<Page<TrainingRecord>> = await trainingRecordApi.find({candidateId: props.id} as TrainingRecordQueryForm)
    if (resp && resp.code === 1) {
      if (resp.data) {
      const page: Page<TrainingRecord> = resp.data
        if (page && page.list.length > 0) {
          trainingRecordState.tableData = page.list
          trainingRecordState.total = page.total
        }
      }
    }
  } catch (e) {
    console.log('获取数据异常', e)
  } finally {
    trainingRecordState.loading = false
  }
}

const onBack = () => {
  emit('closeCandidateViewAllEvent')
}
</script>

<style lang="scss">
</style>